function main() {
  var canvas = document.getElementById('android3D');

  var gl = WebGLUtils.setupWebGL(canvas);
  if (!gl) {
    console.log('Failed to get the rendering context for WebGL');
    return;
  }
  var draw3D0 = new Draw3DTex(gl, 'cyclinder');

  gl.clearColor(0.0, 0.0, 0.0, 0.0);
  gl.enable(gl.DEPTH_TEST);
  gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);


  draw3D0.drawCylinder(
      gl, [0, 1, 0, 1], [0, -1, 0, 1], 1, [52 / 0xFF, 168 / 0xFF, 83 / 0xFF],
      canvas);


  //   document.onkeydown = function(ev) {
  //     if (ev.keyCode == 37) {  // left
  //       gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
  //     } else if (ev.keyCode == 39) {  // right
  //       gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
  //       draw3D0.updateCylinder(gl, [0, 1, 0, 1], [0, -1, 0, 1]);
  //       draw3D1.updateBall(gl, [0, -3, 0, 1], [1, 1, 0]);

  //     } else if (ev.keyCode == 38) {  // up
  //       gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
  //       draw3D1.drawBall(
  //           gl, [0, -2, 0, 1], [1, 1, 0], 0.5, 1,
  //           [52 / 0xFF, 168 / 0xFF, 83 / 0xFF]);
  //       draw3D0.drawCylinder(
  //           gl, [0, 1, 0, 1], [0, -1, 0, 1], 1,
  //           [52 / 0xFF, 168 / 0xFF, 83 / 0xFF]);

  //     } else {
  //       return;
  //     }
  //   };
}

var Draw3DTex = function(gl, type) {
  this.type = type;

  this.program = createProgram(
      gl, this.TEXTURE_VSHADER_SOURCE, this.TEXTURE_FSHADER_SOURCE);
  if (!this.program) {
    console.log('Failed to intialize shaders.');
    return;
  }
  this.program.a_Position = gl.getAttribLocation(this.program, 'a_Position');
  this.program.a_Normal = gl.getAttribLocation(this.program, 'a_Normal');
  this.program.a_TexCoord = gl.getAttribLocation(this.program, 'a_TexCoord');
  this.program.u_MvpMatrix = gl.getUniformLocation(this.program, 'u_MvpMatrix');
  this.program.u_NormalMatrix =
      gl.getUniformLocation(this.program, 'u_NormalMatrix');
  this.program.u_Sampler = gl.getUniformLocation(this.program, 'u_Sampler');
  this.program.u_LightPosition =
      gl.getUniformLocation(this.program, 'u_LightPosition');
  if (this.program.a_Position < 0 || this.program.a_Normal < 0 ||
      this.program.a_TexCoord < 0 || !this.program.u_MvpMatrix ||
      !this.program.u_NormalMatrix || !this.program.u_Sampler) {
    console.log('Failed to get the storage location');
    return;
  }
};
/**
 * 可设置的变量
 */
//图形类型,type为'cyclinder'或'ball'
Draw3DTex.prototype.type = null;

Draw3DTex.prototype.texture;
/**
 * 圆柱的两个圆心
 */
Draw3DTex.prototype.cyclinderVertice1;
Draw3DTex.prototype.cyclinderVertice2;
/**
 * 球的中心点和上方向
 */
Draw3DTex.prototype.ballVertice;
Draw3DTex.prototype.ballVector;
//视点
Draw3DTex.prototype.lookat = new Array(0, 2, 20, 0, 0, 0, 0, 1, 0);
//投影
Draw3DTex.prototype.perspective = new Array(30, 1, 1, 100);
//图形变换矩阵，可以使图形进行变化
Draw3DTex.prototype.MODEL_MATRIX = new Matrix4();

Draw3DTex.prototype.setModelMatrix = function(modelMatrix) {
  this.MODEL_MATRIX = modelMatrix;
};

//着色器的程序对象
Draw3DTex.prototype.program;
/**
 * 缓存区对象
 */
Draw3DTex.prototype.vertexBuffer;
Draw3DTex.prototype.colorBuffer;
Draw3DTex.prototype.normalBuffer;
Draw3DTex.prototype.indexBuffer;
Draw3DTex.prototype.numIndices;
Draw3DTex.prototype.texCoordBuffer;


Draw3DTex.prototype.TEXTURE_VSHADER_SOURCE = 'attribute vec4 a_Position;\n' +
    'attribute vec4 a_Normal;\n' +
    'attribute vec2 a_TexCoord;\n' +
    'uniform mat4 u_MvpMatrix;\n' +
    'uniform mat4 u_NormalMatrix;\n' +
    // 'varying float v_NdotL;\n' +
    'varying vec2 v_TexCoord;\n' +
    'varying vec3 v_Normal;\n' +
    'void main() {\n' +

    // coordinate)
    '  gl_Position = u_MvpMatrix * a_Position;\n' +
    '  v_Normal = normalize(vec3(u_NormalMatrix * a_Normal));\n' +
    // '  v_NdotL = max(dot(normal, lightDirection), 0.0);\n' +
    '  v_TexCoord = a_TexCoord;\n' +
    '}\n';

// Fragment shader for texture drawing
Draw3DTex.prototype.TEXTURE_FSHADER_SOURCE = '#ifdef GL_ES\n' +
    'precision mediump float;\n' +
    '#endif\n' +
    'uniform vec3 u_LightPosition;\n' +  // 光的位置
    'uniform sampler2D u_Sampler;\n' +
    'varying vec2 v_TexCoord;\n' +
    // 'varying float v_NdotL;\n' +
    'varying vec3 v_Normal;\n' +
    'void main() {\n' +
    '  vec3 normal = normalize(v_Normal);\n' +
    '  vec3 lightDirection = u_LightPosition;\n' +  // Light direction(World
    '  float nDotL = max(dot(lightDirection, normal), 0.0);\n' +
    '  vec4 color = texture2D(u_Sampler, v_TexCoord);\n' +
    '  gl_FragColor = vec4(color.rgb * nDotL, color.a);\n' +
    '}\n';


/**
* 初次画圆柱、球
*/
Draw3DTex.prototype.drawCylinder = function(
    gl, vertice1, vertice2, r, colors, canvas) {
  if (this.type != 'cyclinder') {
    console.log('Type is not cyclinder');
    return;
  }
  this.cyclinderVertice1 = vertice1;
  this.cyclinderVertice2 = vertice2;
  //圆柱的上下两点的向量
  var vector2_1 = new Array(3);

  for (var i = 0; i < vector2_1.length; i++) {
    vector2_1[i] = vertice1[i] / vertice1[3] - vertice2[i] / vertice2[3];
  }

  var high = Math.sqrt(
      vector2_1[0] * vector2_1[0] + vector2_1[1] * vector2_1[1] +
      vector2_1[2] * vector2_1[2]);

  //初始化缓存区
  if (!this.cylinderInitVertexBuffers(gl, high, r, colors)) {
    console.log('Failed to set the vertex information');
    return;
  }
  var texture;
  // if (!texture) {
  //   console.log('Failed to intialize the texture.');
  //   return;
  // }


  this.updateCylinder(gl, vertice1, vertice2, texture);



};

Draw3DTex.prototype.initAttributeVariable = function(gl, a_attribute, buffer) {
  gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
  gl.vertexAttribPointer(a_attribute, buffer.num, buffer.type, false, 0, 0);
  gl.enableVertexAttribArray(a_attribute);
};
Draw3DTex.prototype.updateCylinder = function(gl, vertice1, vertice2, texture) {
  gl.useProgram(this.program);  // Tell that this program object is used

  // Assign the buffer objects and enable the assignment
  console.log(this.program);
  this.initAttributeVariable(
      gl, this.program.a_Position, this.vertexBuffer);  // Vertex coordinates
  this.initAttributeVariable(
      gl, this.program.a_Normal, this.normalBuffer);  // Normal
  this.initAttributeVariable(
      gl, this.program.a_TexCoord, this.texCoordBuffer);  // Texture coordinates
  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);  // Bind indices

  // Bind texture object to texture unit 0
  gl.activeTexture(gl.TEXTURE0);
  gl.bindTexture(gl.TEXTURE_2D, texture);
  gl.uniform3f(this.program.u_LightPosition, 2.3, 4.0, 3.5);

  var mvpMatrix = new Matrix4();
  var normalMatrix = new Matrix4();

  mvpMatrix.setPerspective(
      this.perspective[0], this.perspective[1], this.perspective[2],
      this.perspective[3]);
  mvpMatrix.lookAt(
      this.lookat[0], this.lookat[1], this.lookat[2], this.lookat[3],
      this.lookat[4], this.lookat[5], this.lookat[6], this.lookat[7],
      this.lookat[8]);


  var image = document.getElementById('texImage');

  this.configureTexture(gl, image);
  var modelMatrix = new Matrix4();
  mvpMatrix.multiply(modelMatrix);
  normalMatrix.setInverseOf(modelMatrix);
  normalMatrix.transpose();

  gl.uniformMatrix4fv(this.program.u_ModelMatrix, false, modelMatrix.elements);

  gl.uniformMatrix4fv(this.program.u_MvpMatrix, false, mvpMatrix.elements);

  gl.uniformMatrix4fv(
      this.program.u_NormalMatrix, false, normalMatrix.elements);
  // this.gl.clear(this.gl.COLOR_BUFFER_BIT | this.gl.DEPTH_BUFFER_BIT);
  gl.drawElements(gl.TRIANGLES, this.numIndices, this.indexBuffer.type, 0);

};

Draw3DTex.prototype.cylinderInitVertexBuffers = function(gl, high, r, color) {
  // 上下两个圆面的y坐标
  var y1 = high / 2;
  var y2 = (-1) * high / 2;
  var tmpColorOrTex = 1.0;
  var num = 240;                              //一个园面的扇形数
  var vertice_num = ((num + 1) * 3 + 1) * 2;  //点数量
  //点位置
  var vertices = new Float32Array(vertice_num * 3);
  //点颜色
  var colors = new Float32Array(vertice_num * 3);
  //法向量
  var normals = new Float32Array(vertice_num * 3);
  //点索引
  var indices = new Uint16Array(num * 4 * 3);


  //颜色或纹理判断值
  var colorOrTexs = [];
  //纹理图片坐标
  var texCoords = new Float32Array(vertice_num * 2);
  var texH, texL;
  if (high > (2 * Math.PI * r)) {
    texH = 1;
    texL = (2 * Math.PI * r) / high;
  } else {
    texH = high / (2 * Math.PI * r);
    texL = 1;
  }
  console.log(texH);

  console.log(texL);
  var tmpTexCoord = new Array();
  var tex_index = 0;
  tmpTexCoord[0] = 0;
  tmpTexCoord[1] = 0;
  texCoords.set(tmpTexCoord, tex_index);
  colorOrTexs.push(0);
  tex_index += 2;
  texCoords.set(tmpTexCoord, tex_index);
  colorOrTexs.push(0);
  tex_index += 2;
  for (var i = 0; i < num + 1; i++) {
    tmpTexCoord[0] = i * texL / (num + 1);
    tmpTexCoord[1] = texH;
    texCoords.set(tmpTexCoord, tex_index);
    colorOrTexs.push(tmpColorOrTex);
    tex_index += 2;
    texCoords.set(tmpTexCoord, tex_index);
    colorOrTexs.push(0);
    tex_index += 2;
    texCoords.set(tmpTexCoord, tex_index);
    colorOrTexs.push(tmpColorOrTex);
    tex_index += 2;

    tmpTexCoord[1] = 0;
    texCoords.set(tmpTexCoord, tex_index);
    colorOrTexs.push(tmpColorOrTex);
    tex_index += 2;
    texCoords.set(tmpTexCoord, tex_index);
    colorOrTexs.push(0);
    tex_index += 2;
    texCoords.set(tmpTexCoord, tex_index);
    colorOrTexs.push(tmpColorOrTex);
    tex_index += 2;
  }
  //计算圆上的点
  //上下两个圆面的圆心
  var index = 0;
  var tmpVertice = new Array();
  var tmpNormal1 = new Array();
  var tmpNormal2 = new Array();
  tmpVertice[0] = 0;
  tmpVertice[1] = y1;
  tmpVertice[2] = 0;
  vertices.set(tmpVertice, index);
  colors.set(color, index);
  normals.set([0, 1, 0], index);
  index += 3;

  tmpVertice[1] = y2;
  vertices.set(tmpVertice, index);
  colors.set(color, index);
  normals.set([0, -1, 0], index);
  index += 3;

  //圆上的点
  var aa = 0;
  var a = (1 / num) * Math.PI;
  tmpNormal1[1] = 0;
  tmpNormal2[1] = 0;
  for (var i = 0; i < num + 1; i++) {
    aa = (i / num) * 2 * Math.PI;
    tmpNormal1[0] = Math.cos(aa - a);
    tmpNormal1[2] = Math.sin(aa - a);
    tmpNormal2[0] = Math.cos(aa + a);
    tmpNormal2[2] = Math.sin(aa + a);

    tmpVertice[0] = r * Math.cos(aa);
    tmpVertice[1] = y1;
    tmpVertice[2] = r * Math.sin(aa);


    vertices.set(tmpVertice, index);
    colors.set(color, index);
    normals.set(tmpNormal1, index);
    index += 3;
    vertices.set(tmpVertice, index);
    colors.set(color, index);
    normals.set([0, 1, 0], index);
    index += 3;
    vertices.set(tmpVertice, index);
    colors.set(color, index);
    normals.set(tmpNormal2, index);
    index += 3;

    tmpVertice[1] = y2;
    vertices.set(tmpVertice, index);
    colors.set(color, index);
    normals.set(tmpNormal1, index);
    index += 3;
    vertices.set(tmpVertice, index);
    colors.set(color, index);
    normals.set([0, -1, 0], index);
    index += 3;
    vertices.set(tmpVertice, index);
    colors.set(color, index);
    normals.set(tmpNormal2, index);
    index += 3;
  }

  //计算三角形的组合
  var index = 0;
  var tIndices = new Array(3);

  for (var i = 3; i < vertice_num - 6; i += 6) {
    tIndices[0] = 0;
    tIndices[1] = i;
    tIndices[2] = i + 6;
    indices.set(tIndices, index);
    index += 3;

    tIndices[0] = i + 1;
    tIndices[1] = i + 4;
    tIndices[2] = i + 5;
    indices.set(tIndices, index);
    index += 3;

    tIndices[0] = i + 4;
    tIndices[1] = i + 5;
    tIndices[2] = i + 8;
    indices.set(tIndices, index);
    index += 3;

    tIndices[0] = 1;
    tIndices[1] = i + 3;
    tIndices[2] = i + 9;
    indices.set(tIndices, index);
    index += 3;
  }
  //将数据传入缓存区，并交给buffer对象保存相关数据
  this.vertexBuffer = this.initBufferForObj(gl, vertices, 3, gl.FLOAT);
  //   this.colorBuffer = this.initBufferForObj(gl, colors, 3, gl.FLOAT);

  this.normalBuffer = this.initBufferForObj(gl, normals, 3, gl.FLOAT);
  this.texCoordBuffer = this.initBufferForObj(gl, texCoords, 2, gl.FLOAT);
  this.indexBuffer = this.initIndexBufferForObj(gl, indices, gl.UNSIGNED_SHORT);
  this.numIndices = indices.length;
  // Unbind the buffer object
  gl.bindBuffer(gl.ARRAY_BUFFER, null);
  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);

  return true;
};

Draw3DTex.prototype.configureTexture = function(gl, image) {
  this.texture = gl.createTexture();
  gl.bindTexture(gl.TEXTURE_2D, this.texture);
  gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
  gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, gl.RGB, gl.UNSIGNED_BYTE, image);
  gl.generateMipmap(gl.TEXTURE_2D);
  gl.texParameteri(
      gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST_MIPMAP_LINEAR);
  gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);

  gl.uniform1i(gl.getUniformLocation(this.program, 'u_Sampler'), 0);
};

/**
 * 缓存区创建
 */
Draw3DTex.prototype.initBufferForObj = function(gl, data, num, type) {
  var buffer = gl.createBuffer();  // Create a buffer object
  if (!buffer) {
    console.log('Failed to create the buffer object');
    return null;
  }
  // Write date into the buffer object
  gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
  gl.bufferData(gl.ARRAY_BUFFER, data, gl.STATIC_DRAW);

  // Keep the information necessary to assign to the attribute variable later
  buffer.num = num;
  buffer.type = type;
  return buffer;
};
Draw3DTex.prototype.initIndexBufferForObj = function(gl, data, type) {
  var buffer = gl.createBuffer();
  　  // Create a buffer object
      if (!buffer) {
    console.log('Failed to create the buffer object');
    return null;
  }
  // Write date into the buffer object
  gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffer);
  gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, data, gl.STATIC_DRAW);

  buffer.type = type;

  return buffer;
};